package com.xwj.controller.admin;

import com.xwj.entity.RaRoot;
import com.xwj.service.Impl.*;
import com.xwj.service.holder.RaRoleHolder;
import com.xwj.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by thinkjoy on 2017/8/3.
 */
@RestController
@RequestMapping("/admin/role/")
public class RoleController {

    @Autowired
    private RaRoleServiceImpl raRoleService;
    @Autowired
    private RaRootServiceImpl raRootService;
    @Autowired
    private RaRoleRootServiceImpl raRoleRootService;
    @Autowired
    private RaMenuServiceImpl raMenuService;
    @Autowired
    private RaUserServiceImpl raUserService;

    @RequestMapping("rolelist")
    public ModelAndView rolelist(ModelAndView modelAndView) {
        modelAndView.addObject("roles", RaRoleHolder.getRole());
        modelAndView.addObject("roots", raRootService.findAllRoot());
        modelAndView.setViewName("/admin/role/role");
        return modelAndView;
    }

    @RequestMapping(value = "updateRole", method = RequestMethod.POST)
    public Map<String, String> updateRole(@RequestParam("roleId") int roleId, @RequestParam(value = "rootId", required = false) List<Integer> rootIds) {
        Map<String, String> response = new HashMap<>();
        if (rootIds == null) {
            //清空所有权限
            if (raRoleRootService.findRoleRootByRoleId(roleId).size() > 0) {
                if (raRoleRootService.deleteAllRoot(roleId) > 0) {
                    ShiroUtils.reloadAuthorizing();
                    response.put("msg", "已取消所有权限");
                } else
                    response.put("msg", "操作异常，请重试");
            } else
                response.put("msg", "当前无权限");
        } else {
            Map<String, Object> map = new HashMap<>();
            map.put("roleId", roleId);
            map.put("list", rootIds);
            if (raRoleRootService.insertRoleRoot(map) > 0) {
                ShiroUtils.reloadAuthorizing();
                response.put("msg", "操作成功");
            } else
                response.put("msg", "操作异常，请重试");
        }
        return response;
    }

    @RequestMapping(value = "deleteRole", method = RequestMethod.POST)
    public Map<String, Object> deleteRole(@RequestParam("roleId") int roleId) {
        Map<String, Object> response = new HashMap<>();
        if (raRoleService.findRoleById(roleId).getId() != 1) {
            RaRoleHolder.deleteRoleById(roleId);
            if (raUserService.findUserByRoleId(roleId).size() > 0)
                raUserService.updateRoleWhenRoleDelete(roleId);
            ShiroUtils.reloadAuthorizing();
            response.put("msg", "删除成功");
        } else
            response.put("msg", "不可删除超级管理员");
        return response;
    }

    @RequestMapping(value = "addRole", method = RequestMethod.POST)
    public Map<String, Object> addRole(@RequestParam("roleName") String roleName, @RequestParam("desc") String desc, @RequestParam(value = "rootId", required = false) List<Integer> rootIds) {
        Map<String, Object> response = new HashMap<>();
        if (RaRoleHolder.addRoleInfo(roleName, desc, rootIds))
            response.put("msg", "添加成功");
        else
            response.put("msg", "添加失败，请重试");
        return response;
    }


    @RequestMapping(value = "addRoot", method = RequestMethod.POST)
    public Map<String, Object> addRoot(@RequestParam("rootName") String rootName, @RequestParam("desc") String desc) {
        Map<String, Object> response = new HashMap<>();
        if (raRootService.addRoot(rootName, desc) > 0)
            response.put("msg", "添加成功");
        else
            response.put("msg", "添加失败，请重试");
        return response;
    }

    @RequestMapping(value = "updateRoot", method = RequestMethod.POST)
    public Map<String, Object> updateRoot(RaRoot raRoot) {
        Map<String, Object> response = new HashMap<>();
        if (raRootService.updateRootById(raRoot) > 0) {
            ShiroUtils.reloadAuthorizing();
            response.put("msg", "更新成功");
        } else
            response.put("msg", "更新失败，请重试");
        return response;
    }

    @RequestMapping(value = "deleteRoot", method = RequestMethod.POST)
    public Map<String, Object> deleteRoot(int id) {
        Map<String, Object> response = new HashMap<>();
        if (raRoleRootService.findRoleByRootId(id).size() > 0 || raMenuService.findMenuByRootId(id).size() > 0)
            response.put("msg", "该权限在使用中，不可删除");
        else {
            if (raRootService.deleteRootById(id) > 0)
                response.put("msg", "删除成功");
            else
                response.put("msg", "删除失败，请重试");
        }
        return response;
    }


}
